package com.shizhanzhe.qdd.forward;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.base.BaseFragment;
import com.shizhanzhe.qdd.base.UserBean;
import com.shizhanzhe.qdd.forward.activity.WebActivity;
import com.shizhanzhe.qdd.forward.adapter.ShareAdapter;
import com.shizhanzhe.qdd.forward.bean.ShareList;
import com.shizhanzhe.qdd.home.activity.LoginActivity;
import com.shizhanzhe.qdd.url.Path;
import com.shizhanzhe.qdd.utils.NetBroadcastReceiver;
import com.shizhanzhe.qdd.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.qdd.widget.ErrorCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by YJ on 2019/1/3.
 */
public class ForWardItemFragment extends BaseFragment {
    ListView lvShare;
    ImageView nodata;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private String id;
    private ShareAdapter adapter;

    public static ForWardItemFragment newInstance(String id) {
        Bundle args = new Bundle();
        args.putString("id", id);
        ForWardItemFragment fragment = new ForWardItemFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getString("id");
        }
    }


    @Override
    protected int getLayout() {
        return R.layout.fragment_item_forward;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        nodata = view.findViewById(R.id.nodata);
        lvShare = view.findViewById(R.id.lv_share);
        refresh.setRefreshHeader(new ClassicsHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                list.clear();
                getData();
                refreshLayout.finishRefresh(2000);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.SHARE(getArguments().getString("id"), page), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

                    @Override
                    public void onsendJson(String json) {
                        if (!"1".equals(json) && !"2".equals(json)) {


                                Gson gson = new Gson();
                                ShareList shareList = gson.fromJson(json, ShareList.class);
                                List<ShareList.DataBean> lists = shareList.getData();
                                if (lists.size() == 0) {
                                    Toast.makeText(getContext(), "没有更多了", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                list.addAll(lists);
                                adapter.notifyDataSetChanged();

                        }
                    }
                });
                refreshLayout.finishLoadMore(2000);
            }
        });


        getData();
        lvShare.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (UserBean.uid == 0) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                } else {
                    Intent intent = new Intent(getActivity(), WebActivity.class).putExtra("id", list.get(i).getArtid());
                    getActivity().startActivity(intent);
                }
            }
        });
    }
    @Override
    protected void getData() {
        //type 0 可分享 1 已分享
        OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.SHARE(getArguments().getString("id"), page), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        ShareList shareList = gson.fromJson(json, ShareList.class);
                        list = shareList.getData();
                        if (list.size() == 0) {
                            lvShare.setEmptyView(nodata);
                        } else {
                            adapter = new ShareAdapter(list, getContext());
                            lvShare.setAdapter(adapter);
                        }
                        loadService.showSuccess();
                    } catch (Exception e) {
                        loadService.showCallback(ErrorCallback.class);
                    }
                } else {
                    loadService.showCallback(ErrorCallback.class);
                }
            }
        });
    }

    int page = 1;
    List<ShareList.DataBean> list = new ArrayList<>();



}
